home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH10 / SRC / SURFACE3.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-02  |  15.9 KB  |  542 lines

  1. VERSION 4.00
  2. Begin VB.Form SurfaceForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Surfaces"
  6.    ClientHeight    =   5700
  7.    ClientLeft      =   300
  8.    ClientTop       =   855
  9.    ClientWidth     =   9090
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6390
  21.    KeyPreview      =   -1  'True
  22.    Left            =   240
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   5700
  25.    ScaleWidth      =   9090
  26.    Top             =   225
  27.    Width           =   9210
  28.    Begin VB.CheckBox ShowAxesCheck 
  29.       Caption         =   "Show Axes"
  30.       Height          =   255
  31.       Left            =   7080
  32.       TabIndex        =   17
  33.       Top             =   3960
  34.       Width           =   2055
  35.    End
  36.    Begin VB.CheckBox ShowDataCheck 
  37.       Caption         =   "Show True Data"
  38.       Height          =   255
  39.       Left            =   7080
  40.       TabIndex        =   16
  41.       Top             =   3480
  42.       Width           =   2055
  43.    End
  44.    Begin VB.OptionButton Choice 
  45.       Caption         =   "Saddle"
  46.       Height          =   255
  47.       Index           =   8
  48.       Left            =   7080
  49.       TabIndex        =   15
  50.       Top             =   2880
  51.       Width           =   2055
  52.    End
  53.    Begin VB.OptionButton Choice 
  54.       Caption         =   "Cone"
  55.       Height          =   255
  56.       Index           =   7
  57.       Left            =   7080
  58.       TabIndex        =   14
  59.       Top             =   2520
  60.       Width           =   2055
  61.    End
  62.    Begin VB.OptionButton Choice 
  63.       Caption         =   "Holes"
  64.       Height          =   255
  65.       Index           =   6
  66.       Left            =   7080
  67.       TabIndex        =   13
  68.       Top             =   2160
  69.       Width           =   2055
  70.    End
  71.    Begin VB.TextBox PhiText 
  72.       Height          =   285
  73.       Left            =   3600
  74.       TabIndex        =   12
  75.       Text            =   "0.1570"
  76.       Top             =   5400
  77.       Width           =   855
  78.    End
  79.    Begin VB.TextBox ThetaText 
  80.       Height          =   285
  81.       Left            =   2040
  82.       TabIndex        =   10
  83.       Text            =   "0.6283"
  84.       Top             =   5400
  85.       Width           =   855
  86.    End
  87.    Begin VB.TextBox RText 
  88.       Height          =   285
  89.       Left            =   480
  90.       TabIndex        =   8
  91.       Text            =   "10"
  92.       Top             =   5400
  93.       Width           =   855
  94.    End
  95.    Begin VB.OptionButton Choice 
  96.       Caption         =   "Hemisphere"
  97.       Height          =   255
  98.       Index           =   5
  99.       Left            =   7080
  100.       TabIndex        =   7
  101.       Top             =   1800
  102.       Width           =   2055
  103.    End
  104.    Begin VB.OptionButton Choice 
  105.       Caption         =   "Randomized Ridges"
  106.       Height          =   255
  107.       Index           =   4
  108.       Left            =   7080
  109.       TabIndex        =   6
  110.       Top             =   1440
  111.       Width           =   2055
  112.    End
  113.    Begin VB.OptionButton Choice 
  114.       Caption         =   "Ridges"
  115.       Height          =   255
  116.       Index           =   3
  117.       Left            =   7080
  118.       TabIndex        =   5
  119.       Top             =   1080
  120.       Width           =   2055
  121.    End
  122.    Begin VB.OptionButton Choice 
  123.       Caption         =   "Bowl"
  124.       Height          =   255
  125.       Index           =   2
  126.       Left            =   7080
  127.       TabIndex        =   4
  128.       Top             =   720
  129.       Width           =   2055
  130.    End
  131.    Begin VB.OptionButton Choice 
  132.       Caption         =   "Mounds"
  133.       Height          =   255
  134.       Index           =   1
  135.       Left            =   7080
  136.       TabIndex        =   3
  137.       Top             =   360
  138.       Width           =   2055
  139.    End
  140.    Begin VB.OptionButton Choice 
  141.       Caption         =   "Splash"
  142.       Height          =   255
  143.       Index           =   0
  144.       Left            =   7080
  145.       TabIndex        =   2
  146.       Top             =   0
  147.       Value           =   -1  'True
  148.       Width           =   2055
  149.    End
  150.    Begin VB.PictureBox Pict 
  151.       AutoRedraw      =   -1  'True
  152.       Height          =   5295
  153.       Left            =   0
  154.       ScaleHeight     =   349
  155.       ScaleMode       =   3  'Pixel
  156.       ScaleWidth      =   461
  157.       TabIndex        =   0
  158.       Top             =   0
  159.       Width           =   6975
  160.    End
  161.    Begin MSComDlg.CommonDialog LoadDialog 
  162.       Left            =   7080
  163.       Top             =   4560
  164.       _version        =   65536
  165.       _extentx        =   847
  166.       _extenty        =   847
  167.       _stockprops     =   0
  168.       cancelerror     =   -1  'True
  169.    End
  170.    Begin VB.Label Label1 
  171.       Caption         =   "Phi"
  172.       Height          =   255
  173.       Index           =   2
  174.       Left            =   3240
  175.       TabIndex        =   11
  176.       Top             =   5400
  177.       Width           =   375
  178.    End
  179.    Begin VB.Label Label1 
  180.       Caption         =   "Theta"
  181.       Height          =   255
  182.       Index           =   1
  183.       Left            =   1440
  184.       TabIndex        =   9
  185.       Top             =   5400
  186.       Width           =   495
  187.    End
  188.    Begin VB.Label Label1 
  189.       Caption         =   "R"
  190.       Height          =   255
  191.       Index           =   0
  192.       Left            =   240
  193.       TabIndex        =   1
  194.       Top             =   5400
  195.       Width           =   255
  196.    End
  197.    Begin VB.Menu mnuFile 
  198.       Caption         =   "&File"
  199.       Begin VB.Menu mnuFileLoad 
  200.          Caption         =   "&Load..."
  201.          Shortcut        =   ^L
  202.       End
  203.       Begin VB.Menu mnuFileSaveAs 
  204.          Caption         =   "&Save As..."
  205.          Shortcut        =   ^A
  206.       End
  207.       Begin VB.Menu mnuFileSep 
  208.          Caption         =   "-"
  209.       End
  210.       Begin VB.Menu mnuFileExit 
  211.          Caption         =   "E&xit"
  212.       End
  213.    End
  214. Attribute VB_Name = "SurfaceForm"
  215. Attribute VB_Creatable = False
  216. Attribute VB_Exposed = False
  217. Option Explicit
  218. ' Location of viewing eye.
  219. Dim EyeR As Single
  220. Dim EyeTheta As Single
  221. Dim EyePhi As Single
  222. Const Dtheta = PI / 20
  223. Const Dphi = PI / 20
  224. Const Dr = 1
  225. ' Location of focus point.
  226. Const FocusX = 0#
  227. Const FocusY = 0#
  228. Const FocusZ = 0#
  229. Dim Projector(1 To 4, 1 To 4) As Single
  230. Dim ThePicture As ObjPicture
  231. Dim ShowingParameters As Boolean
  232. Dim ChoiceNum As Integer
  233. Dim Sparse As ObjSparseGrid
  234. ' *******************************************************
  235. ' Rotate the points in the cube and draw the cube.
  236. ' *******************************************************
  237. Private Sub DrawData(pic As Object)
  238. Dim x As Single
  239. Dim y As Single
  240. Dim z As Single
  241. Dim S(1 To 4, 1 To 4) As Single
  242. Dim t(1 To 4, 1 To 4) As Single
  243. Dim ST(1 To 4, 1 To 4) As Single
  244. Dim PST(1 To 4, 1 To 4) As Single
  245.     MousePointer = vbHourglass
  246.     Refresh
  247.     ' Prevent overflow errors when drawing lines
  248.     ' too far out of bounds.
  249.     On Error Resume Next
  250.     ' Scale and translate so it looks OK in pixels.
  251.     m3Scale S, 35, -35, 1
  252.     m3Translate t, 230, 175, 0
  253.     m3MatMultiplyFull ST, S, t
  254.     m3MatMultiplyFull PST, Projector, ST
  255.     ' Transform the points.
  256.     ThePicture.ApplyFull PST
  257.     ' Display the data.
  258.     pic.Cls
  259.     ThePicture.Draw pic, EyeR
  260.     pic.Refresh
  261.     ' Display the viewnig parameters.
  262.     ShowViewingParameters
  263.     MousePointer = vbDefault
  264. End Sub
  265. Sub ShowViewingParameters()
  266.     ShowingParameters = True
  267.     RText.Text = Format$(EyeR, "0.0000")
  268.     ThetaText.Text = Format$(EyeTheta, "0.0000")
  269.     PhiText.Text = Format$(EyePhi, "0.0000")
  270.     RText.Refresh
  271.     ThetaText.Refresh
  272.     PhiText.Refresh
  273.     ShowingParameters = False
  274. End Sub
  275. Private Sub Choice_Click(Index As Integer)
  276.     ChoiceNum = Index
  277.     CreateData (ShowAxesCheck.value = vbChecked)
  278.     DrawData Pict
  279.     Pict.SetFocus
  280. End Sub
  281. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  282.     Select Case KeyCode
  283.         Case vbKeyLeft
  284.             EyeTheta = EyeTheta - Dtheta
  285.         
  286.         Case vbKeyRight
  287.             EyeTheta = EyeTheta + Dtheta
  288.         
  289.         Case vbKeyUp
  290.             EyePhi = EyePhi - Dphi
  291.         
  292.         Case vbKeyDown
  293.             EyePhi = EyePhi + Dphi
  294.                 
  295.         Case Else
  296.             Exit Sub
  297.     End Select
  298.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  299.     DrawData Pict
  300. End Sub
  301. Private Sub Form_KeyPress(KeyAscii As Integer)
  302.     Select Case KeyAscii
  303.         Case Asc("+")
  304.             EyeR = EyeR + Dr
  305.         
  306.         Case Asc("-")
  307.             EyeR = EyeR - Dr
  308.         
  309.         Case Else
  310.             Exit Sub
  311.     End Select
  312.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  313.     DrawData Pict
  314. End Sub
  315. Private Sub Form_Load()
  316.     ' Initialize the eye position.
  317.     EyeR = 10
  318.     EyeTheta = PI * 0.2
  319.     EyePhi = PI * 0.1
  320.     ' Initialize the projection transformation.
  321.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  322.     ' Create the data.
  323.     CreateData (ShowAxesCheck.value = vbChecked)
  324.     ' Project and draw the data.
  325.     Me.Show
  326.     DrawData Pict
  327. End Sub
  328. ' ************************************************
  329. ' Create the surface.
  330. ' ************************************************
  331. Sub CreateData(show_axes As Boolean)
  332. Const Xmin = -5
  333. Const Zmin = -5
  334. Const Xmax = -Xmin
  335. Const Zmax = -Zmin
  336. Const Dx = 0.3
  337. Const Dz = 0.3
  338. Const NumX = -2 * Xmin / Dx
  339. Const NumZ = -2 * Zmin / Dz
  340. Const Amp = 0.25
  341. Const Per = 2 * PI / 4
  342. Const Amp2 = 1
  343. Const Per2 = 2 * PI / 16
  344. Const Amp3 = 2
  345. Const NUM_PTS = NumX * NumZ / 4
  346. Dim axis As ObjPolyline
  347. Dim i As Integer
  348. Dim x As Single
  349. Dim y As Single
  350. Dim z As Single
  351. Dim D As Single
  352. Dim R2 As Single
  353. Dim x1 As Single
  354. Dim z1 As Single
  355. Dim x2 As Single
  356. Dim z2 As Single
  357.     MousePointer = vbHourglass
  358.     Refresh
  359.     Set ThePicture = New ObjPicture
  360.     Set Sparse = New ObjSparseGrid
  361.     Sparse.ShowTrueData = (showdatacheck.value = vbChecked)
  362.     ThePicture.objects.Add Sparse
  363.     If show_axes Then
  364.         Set axis = New ObjPolyline
  365.         ThePicture.objects.Add axis
  366.         axis.AddSegment 0, 0, 0, 5.5, 0, 0
  367.         axis.AddSegment 0, 0, 0, 0, 3, 0
  368.         axis.AddSegment 0, 0, 0, 0, 0, 5.5
  369.     End If
  370.     R2 = (Xmin + 3 * Dx) * (Xmin + 3 * Dx)
  371.     For i = 1 To NUM_PTS
  372.         x = (Xmax - Xmin) * Rnd + Xmin
  373.         z = (Zmax - Zmin) * Rnd + Zmin
  374.         Select Case ChoiceNum
  375.             Case 0  ' Splash.
  376.                 D = Sqr(x * x + z * z)
  377.                 y = Amp * Cos(3 * D)
  378.             
  379.             Case 1  ' Mounds.
  380.                 y = Amp * (Cos(Per * x) + Cos(Per * z))
  381.             
  382.             Case 2  ' Bowl.
  383.                 y = 0.2 * (x * x + z * z) - 5#
  384.             
  385.             Case 3  ' Ridges.
  386.                 y = Amp2 * Cos(Per2 * x) + Amp3 * Cos(Per * z) / (Abs(z) / 3 + 1)
  387.         
  388.             Case 4  ' Random ridges.
  389.                 y = Amp2 * Cos(Per2 * x) + Amp3 * Cos(Per * z) / (Abs(z) / 3 + 1) + Amp * Rnd
  390.         
  391.             Case 5  ' Hemisphere.
  392.                 D = x * x + z * z
  393.                 If D >= R2 Then
  394.                     y = 0
  395.                 Else
  396.                     y = Sqr(R2 - D)
  397.                 End If
  398.             
  399.             Case 6  ' Holes.
  400.                 x1 = (x + Xmin / 2)
  401.                 z1 = (z + Xmin / 2)
  402.                 x2 = (x - Xmin / 2)
  403.                 z2 = (z - Xmin / 2)
  404.                 y = Amp3 - _
  405.             1 / (x1 * x1 + z1 * z1 + 0.1) - _
  406.             1 / (x2 * x2 + z1 * z1 + 0.1) - _
  407.             1 / (x1 * x1 + z2 * z2 + 0.1) - _
  408.             1 / (x2 * x2 + z2 * z2 + 0.1)
  409.         
  410.             Case 7  ' Cone.
  411.                 y = 2 * (Amp3 - Sqr(x * x + z * z))
  412.                 If y < -Amp3 Then y = -Amp3
  413.         
  414.             Case 8  ' Saddle.
  415.                 y = (x * x - z * z) / 10
  416.             
  417.         End Select
  418.         
  419.         Sparse.SetValue x, y, z
  420.     Next i
  421.     ' Create the grid data.
  422.     Sparse.InitializeGrid Dx, Dz
  423.     ' The MousePointer will be reset when we draw.
  424. End Sub
  425. Private Sub mnuFileExit_Click()
  426.     Unload Me
  427. End Sub
  428. Private Sub mnuFileLoad_Click()
  429. Dim fname As String
  430. Dim filenum As Integer
  431. Dim txt As String
  432. Dim Xmin As Single
  433. Dim Ymin As Single
  434. Dim Xmax As Single
  435. Dim Ymax As Single
  436.     ' Allow the user to pick a file.
  437.     On Error Resume Next
  438.     LoadDialog.filename = "*.APF"
  439.     LoadDialog.ShowOpen
  440.     If Err.Number = cdlCancel Then
  441.         Unload LoadDialog
  442.         Exit Sub
  443.     ElseIf Err.Number <> 0 Then
  444.         Unload LoadDialog
  445.         Beep
  446.         MsgBox "Error selecting file.", , vbExclamation
  447.         Exit Sub
  448.     End If
  449.     On Error GoTo 0
  450.     MousePointer = vbHourglass
  451.     DoEvents
  452.     fname = LoadDialog.filename
  453.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  454.         - Len(LoadDialog.FileTitle) - 1)
  455.     ' Clear the picture.
  456.     Set ThePicture = Nothing
  457.     ' Open the file.
  458.     filenum = FreeFile
  459.     Open fname For Input As #filenum
  460.     ' Make sure it's an Object Picture File.
  461.     Input #filenum, txt
  462.     If txt <> "3D APF PICTURE" Then
  463.         Close filenum
  464.         Beep
  465.         MsgBox "Error reading file """ & fname & """.", , vbExclamation
  466.         Exit Sub
  467.     End If
  468.     ' Read the picture.
  469.     Set ThePicture = New ObjPicture
  470.     ThePicture.FileInput filenum
  471.     ' Close the file.
  472.     Close filenum
  473.     ' Refresh the display.
  474.     DrawData Pict
  475.     ' Deselect all the option buttons.
  476.     For ChoiceNum = 0 To 8
  477.         If Choice(ChoiceNum).value Then _
  478.             Choice(ChoiceNum).value = False
  479.     Next ChoiceNum
  480.     MousePointer = vbDefault
  481. End Sub
  482. Private Sub mnuFileSaveAs_Click()
  483. Dim fname As String
  484. Dim filenum As Integer
  485.     ' Allow the user to pick a file.
  486.     On Error Resume Next
  487.     LoadDialog.filename = "*.APF"
  488.     LoadDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  489.     LoadDialog.ShowSave
  490.     If Err.Number = cdlCancel Then
  491.         Unload LoadDialog
  492.         Exit Sub
  493.     ElseIf Err.Number <> 0 Then
  494.         Unload LoadDialog
  495.         Beep
  496.         MsgBox "Error selecting file.", , vbExclamation
  497.         Exit Sub
  498.     End If
  499.     On Error GoTo 0
  500.     fname = LoadDialog.filename
  501.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  502.         - Len(LoadDialog.FileTitle) - 1)
  503.     ' Open the file.
  504.     filenum = FreeFile
  505.     Open fname For Output As #filenum
  506.     ' Write the picture.
  507.     ThePicture.FileWrite filenum
  508.     ' Close the file.
  509.     Close filenum
  510. End Sub
  511. Private Sub PhiText_Change()
  512.     If ShowingParameters Then Exit Sub
  513.     EyePhi = CSng(PhiText.Text)
  514.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  515.     DrawData Pict
  516. End Sub
  517. Private Sub RText_Change()
  518.     If ShowingParameters Then Exit Sub
  519.     EyeR = CSng(RText.Text)
  520.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  521.     DrawData Pict
  522. End Sub
  523. Private Sub ShowAxesCheck_Click()
  524.     CreateData (ShowAxesCheck.value = vbChecked)
  525.     DrawData Pict
  526.     Pict.SetFocus
  527. End Sub
  528. ' ************************************************
  529. ' Turn the drawing of the actual data on/off.
  530. ' ************************************************
  531. Private Sub ShowDataCheck_click()
  532.     Sparse.ShowTrueData = (showdatacheck.value = vbChecked)
  533.     DrawData Pict
  534.     Pict.SetFocus
  535. End Sub
  536. Private Sub ThetaText_Change()
  537.     If ShowingParameters Then Exit Sub
  538.     EyeTheta = CSng(ThetaText.Text)
  539.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  540.     DrawData Pict
  541. End Sub
  542.